﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JTB.DAL;
using JTB.BLL;
using System.Data;
using System.Configuration;

namespace JTB.JobHost.HZ
{
    class Program
    {
        static string serviceAddressHz = ConfigurationManager.AppSettings["JtbHzApiHost"];
        static string serviceAddressGz = ConfigurationManager.AppSettings["JtbGzApiHost"];
        static void Main(string[] args)
        {
            JTBHZ hz = new JTBHZ();
            var tableNameList = hz.GetTableNameList();
            var tableList = hz.GetToSyncTableList(tableNameList);
            var syncSuccessList = new List<DataTable>();
            HttpRequestService httpRequestService = new HttpRequestService();
            foreach (var table in tableList)
            {
                if (table != null && table.Select() != null && table.Select().Length > 0 && methodMapping.ContainsKey(table.TableName))
                {
                    var requestUrl = string.Format("{0}{1}", serviceAddressHz, methodMapping[table.TableName]);

                    var jsonBody = JSON.DatatableToJson(table);
                    var jsonData = string.Format("{0} \"CompanyId\":\"广州悦行\",\"Source\":\"12345678\",\"IPCType\":\"{1}\",\"{1}\":{2}{3}", "{", table.TableName, jsonBody, "}");
                    var res = httpRequestService.Post(requestUrl, jsonData);
                    if (res != null && res.ToLower().Contains("success"))
                    {
                        syncSuccessList.Add(table);
                    }
                }
            }
            var syncCount = hz.UpdateSyncStatus(syncSuccessList);
        }


        static Dictionary<string, string> methodMapping = new Dictionary<string, string>()
        {
            { "BaseInfoCompany","/BaseInfo/Company"},
            { "BaseInfoCompanyStat","/BaseInfo/CompanyStat"},
            { "BaseInfoCompanyPay","/BaseInfo/CompanyPay"},
            { "BaseInfoCompanyService","/BaseInfo/CompanyService"},
            { "BaseInfoCompanyPermit","/BaseInfo/CompanyPermit"},
            { "BaseInfoCompanyFare","/BaseInfo/CompanyFare"},
            { "BaseInfoVehicle","/BaseInfo/Vehicle"},
            { "BaseInfoVehicleInsurance","/BaseInfo/VehicleInsurance"},
            { "BaseInfoVehicleTotalMile","/BaseInfo/VehicleTotalMile"},
            { "BaseInfoDriver","/BaseInfo/Driver"},
            { "BaseInfoDriverEducate","/BaseInfo/DriverEducate"},
            { "BaseInfoDriverApp","/BaseInfo/DriverApp"},
            { "BaseInfoDriverStat","/BaseInfo/DriverStat"},
            { "BaseInfoPassenger","/BaseInfo/Passenger"},
            { "RatedPassenger","/Rated/Passenger"},
            { "OrderMatch","/Order/Match"},
            { "OrderCancel","/Order/Cancel"},
            { "OperateLogin","/Operate/Login"},
            { "OperateLogout","/Operate/Logout"},
            { "OperateDepart","/Operate/Depart"},
            { "OperateArrive","/Operate/Arrive"},
            { "OperatePay","/Operate/Pay"},
            { "PositionDriver","/Position/Driver"},
            { "PositionVechicle","/Position/Vechicle"},
            { "RatedPassengerComplaint","/Rated/PassengerComplaint"},
            { "RatedDriverPunish","/Rated/DriverPunish"},
            { "RatedDriver","/Rated/Driver"},
            { "ShareCompany","/Share/Company"},
            { "ShareRoute","/Share/Route"},
            { "ShareOrder","/Share/Order"},
            { "SharePay","/Share/Pay"}
        };
    }
}
